home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / allison / date4.cpp < prev    next >
C/C++ Source or Header  |  1995-03-12  |  1KB  |  54 lines

  1. LISTING 11 - Implementation for Listing 10
  2. // date4.cpp
  3.  
  4. #include <stdio.h>
  5. #include "date3.h"
  6.  
  7. struct DateRep
  8. {
  9.     DateRep(int, int, int);
  10.  
  11. private:
  12.     friend struct Date;
  13.  
  14.     int month;
  15.     int day;
  16.     int year;
  17. };
  18.  
  19. const char * Date::month_text[13] =
  20.     {"Bad month", "January", "February", "March", "April",
  21.      "May", "June", "July", "August", "September",
  22.      "October", "November", "December"};
  23.  
  24. DateRep::DateRep(int m, int d, int y) : month(m), day(d), year(y)
  25. {}
  26.  
  27. Date::Date(int m, int d, int y)
  28. {
  29.     drep = new DateRep(m,d,y);
  30. }
  31.  
  32. Date::~Date()
  33. {
  34.     delete drep;
  35. }
  36.  
  37. char * Date::format(char *buf) const
  38. {
  39.     sprintf(buf,"%s %d, %d",
  40.             month_text[drep->month],
  41.             drep->day,drep->year);
  42.     return buf;
  43. }
  44.  
  45. int Date::compare(const Date & dp2) const
  46. {
  47.     int result = drep->year - dp2.drep->year;
  48.     if (result == 0)
  49.         result = drep->month - dp2.drep->month;
  50.     if (result == 0)
  51.         result = drep->day - dp2.drep->day;
  52.     return result;
  53. }
  54.